<?php
namespace app\protocol\commands;

class DivCommand extends BaseCommand
{
    public function execute($params, $connection): string
    {
        $args = $this->parseParams($params);
        $this->validateArgumentCount($args, 2, self::getUsage());

        $x = $this->validateNumeric($args[0], '被除数');
        $y = $this->validateNumeric($args[1], '除数');

        if ($y == 0) {
            throw new \InvalidArgumentException('除数不能为零');
        }

        $result = $x / $y;

        return (string)$result;
    }

    public static function getDescription(): string
    {
        return '除法运算: div <x> <y>';
    }

    public static function getUsage(): string
    {
        return 'div 9 4';
    }
}